﻿

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加项目</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link href="../../../layuiadmin/style/mystyle.css" rel="stylesheet" />
    <script src="../../../layuiadmin/artdialog/artDialog.js?skins=chrome"></script>
    <script src="../../../layuiadmin/lib/jquery.min.js"></script>
    <script src="../../../layuiadmin/lib/api.js"></script>
    <script src="../../../layuiadmin/lib/common.js"></script>
    <!--编辑器-->
    <link href="../../../layuiadmin/kindeditor/themes/default/default.css" rel="stylesheet" />
    <script src="../../../layuiadmin/kindeditor/kindeditor-all.js"></script>
    <script src="../../../layuiadmin/kindeditor/lang/zh-CN.js"></script>
    <script src="../../../layuiadmin/layui/layui.js"></script>
    <script src="../../../layuiadmin/lib/system.js"></script>
    <script>
        //检查服务器是否已登录，没有登录则跳转至登录页。
        sys_islogin();
    </script>
</head>
<body>
    
    <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
        <div style="margin-bottom:45px;">
            <input type="hidden" id="ID" name="ID" value="" />
            <input type="hidden" id="ATTACHCODE" name="ATTACHCODE" value="" />
            <div class="layui-form-item">
                <label class="layui-form-label">项目标题</label>
                <div class="layui-input-inline">
                    <input style="width:450px;" type="text" name="PROJECTNAME" id="PROJECTNAME" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">项目负责人</label>
                <div class="layui-input-inline">
                    <input style="width:250px;cursor:pointer;" id="LEADERNAME" type="text" readonly name="LEADERNAME" placeholder="请选择" autocomplete="off" class="layui-input">
                    <input type="hidden" id="PROJECTLEADER" name="PROJECTLEADER" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">项目成员</label>
                <div class="layui-input-inline">
                    <input style="width:250px;cursor:pointer;" id="MEMBERNAME" type="text" name="MEMBERNAME" readonly placeholder="请选择" autocomplete="off" class="layui-input">
                    <input type="hidden" id="PROJIECTMEMBER" name="PROJIECTMEMBER" />
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">选择日期</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="STARTTIME" name="STARTTIME" placeholder="开始日期">
                    </div>
                    <div class="layui-form-mid">
                        -
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="ENDTIME" name="ENDTIME" placeholder="结束日期">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上传文件</label>
                <div class="layui-input-inline" style="width:80%;">
                    <button type="button" class="layui-btn" id="file-upload"><i class="layui-icon"></i>上传文件</button>
                    <ul id="filelist" class="list-inline" style="margin-top:10px;">
                    </ul>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">项目简介</label>
                <div>
                    <textarea name="PROJECTDESC" id="PROJECTDESC" style="width: 80%; height: 500px;" autocomplete="off" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>
        

        <div class="layui-form-item layui-layout-admin" style="text-align:center;">
            <div class="layui-footer-new" style="background-color:#fff;">
                <button class="layui-btn" lay-submit lay-filter="save" id="save">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">取消</button>
            </div>
        </div>
    </div>

    
    <script>
        //编辑器
        var editor;
        KindEditor.ready(function (K) {
            editor = K.create('textarea[name="PROJECTDESC"]', {
                allowImageUpload: true,
                uploadJson: '/KindEditorMerber/UpLoadFiles',
                fileManagerJson: '/KindEditorMerber/FileManager',
                items: [
                    'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                    'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                    'insertunorderedlist', '|', 'emoticons', 'image', 'link']
            });

        });
        layui.config({
            base: '../../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'laydate', 'upload'], function () {
            var $ = layui.$
                , form = layui.form, upload = layui.upload;
            
            //监听提交
            form.on('submit(save)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                data.field.PROJECTDESC = editor.html(); //获取编辑框的值
                console.log(field.STARTTIME + "--" + field.ENDTIME);
                if (field.STARTTIME && field.ENDTIME) {
                    if (field.STARTTIME > field.ENDTIME) {
                        layer.msg("开始时间不能大于结束时间，请重新选择");
                        return;
                    }
                }
                
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: "/ProjectMerber/AddOrUpdateProject",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                    data: { arg: JSON.stringify(field)},    //参数值
                    type: "POST",   //请求方式
                    beforeSend: function () {
                        //请求前的处理
                    },
                    success: function (data) {
                        //请求成功时处理
                        if (data.S == 1) {
                            layer.msg("项目添加成功");
                            parent.layui.table.reload('LAY-app-content-list'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }
                        else {
                            layer.msg("项目添加失败，请稍后重试");
                        }

                    },
                    
                    error: function (res) {
                        layer.msg("项目添加失败，请联系技术人员");
                    }
                });

            });

            //选择项目负责人
            $('#LEADERNAME').on('click', function () {
                layer.open({
                    type: 2
                    , title: '选择项目负责人'
                    , content: '../../user/user/userselect.html'
                    , maxmin: true
                    , area: ['80%', '500px']
                    , success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        // 向子页面的全局函数child_select_user传参
                        iframe.child_select_user("LEADERNAME", "PROJECTLEADER", $("#PROJECTLEADER").val());
                    }
                });
            });

            //选择项目成员
            $('#MEMBERNAME').on('click', function () {
                layer.open({
                    type: 2
                    , title: '选择项目成员'
                    , content: '../../user/user/userselect.html'
                    , maxmin: true
                    , area: ['80%', '500px']

                    , success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        // 向子页面的全局函数child_select_user传参
                        iframe.child_select_user("MEMBERNAME", "PROJIECTMEMBER", $("#PROJIECTMEMBER").val());
                    }
                });
            });

            var laydate = layui.laydate;
            //开始日期
            var insStart = laydate.render({
                elem: '#STARTTIME'
                //, min: 0
                //, type: 'datetime'
                
                , done: function (value, date) {
                    //更新结束日期的最小日期
                    insEnd.config.min = lay.extend({}, date, {
                        month: date.month - 1
                    });

                    //自动弹出结束日期的选择器
                    insEnd.config.elem[0].focus();
                }
            });

            //结束日期
            var insEnd = laydate.render({
                elem: '#ENDTIME'
               // , min: 0
                //, type: 'datetime'
                , done: function (value, date) {
                    //更新开始日期的最大日期
                    insStart.config.max = lay.extend({}, date, {
                        month: date.month - 1
                    });
                }
            });

            //上传文件
            var uploadInst = upload.render({
                elem: '#file-upload'
                , url: '/FileMerber/UpLoadFiles'
                , accept: 'file'
                , multiple: true
                , done: function (res) {
                     
                    if (res.flat == 1) {
                        //上传成功,修改数据库的数据
                        if (!$("#ATTACHCODE").val()) $("#ATTACHCODE").val(common.newGuid(32));
                        $.ajax({
                            url: "/FileMerber/updateFile",    //请求的url地址
                            dataType: "json",   //返回格式为json
                            async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
                            data: { arg: JSON.stringify(res.list), attachcode: $("#ATTACHCODE").val(), operation: 'update' },    //参数值
                            type: "POST",   //请求方式
                            success: function (res) {
                                 
                                if (res.result > 0) {
                                    var html = "";
                                    for (var i = 0; i < res.list.length; i++) {
                                        html += "<li id=\"" + res.list[i].id + "\"  style=\"height:30px;margin-bottom:10px;\">";
                                        html += "<div class=\"span3\" style=\"font-size: 20px;\">" + res.list[i].filename;
                                        html += "<button onclick=\"deluploadfile('" + res.list[i].id + "')\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;'><i class=\"layui-icon\">&#xe640;</i></button>";
                                        html += "<a target='_blank' href=\"" + res.list[i].filepath + "\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;margin-right:10px;'><i class=\"layui-icon\">&#xe601;</i></a>";
                                        html += "</div>";
                                        html += "</li>"; 
                                    }
                                     
                                    $("#filelist").append(html);
                                }
                                else layer.msg("文件上传失败，请稍后重试");
                            },
                            error: function (res) {
                                console.log(res);
                                layer.msg("上传失败，请联系技术人员");
                            }
                        });
                        
                    }
                    else {
                        //上传失败
                        layer.msg(res.message);
                    }
                }
                , error: function (res) {
                    console.log(res);
                    layer.msg("上传失败，请联系技术人员");
                }
            });

        })

        //删除上传文件div
        var deluploadfile = function (id) {
            layer.confirm('确定删除文件吗？', function (index) {
                $.ajax({
                    url: "/FileMerber/DeleteFile",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
                    data: { id: id },    //参数值
                    type: "POST",   //请求方式
                    success: function (res) {
                        if (res.result > 0) {
                            $('#' + id).remove();
                        }
                        else {
                            layer.msg("文件删除失败，请稍后重试");
                        }
                    },

                    error: function (res) {
                        console.log(res);
                        layer.msg("文件删除失败，请联系技术人员");
                    }
                });
                layer.close(index);
            });
        };

        //接收父页面的数据
        function child_fill_data(data) {
            //console.log(data);
            for (let key in data) {
                $("#" + key.toUpperCase()).val(data[key]);
                if (key.toUpperCase() == 'PROJECTDESC') //设置编辑框的值
                    editor.html(data[key]);
            }
            
            $("#STARTTIME").val(common.SubString($("#STARTTIME").val(), 0, 10));
            $("#ENDTIME").val(common.SubString($("#ENDTIME").val(), 0, 10));
            if ($("#ATTACHCODE").val()) {
                $.ajax({
                    url: "/FileMerber/GetFileAttachCode",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
                    data: { attachCode: $("#ATTACHCODE").val()},    //参数值
                    type: "POST",   //请求方式
                    success: function (res) {
                        console.log(res);
                        if (res.flag > 0) {
                            if (res.list.length > 0) {
                                var html = "";
                                for (var i = 0; i < res.list.length; i++) {
                                    html += "<li id=\"" + res.list[i].id + "\"  style=\"height:30px;margin-bottom:10px;\">";
                                    html += "<div class=\"span3\" style=\"font-size: 20px;\">" + res.list[i].filename;
                                    html += "<button onclick=\"deluploadfile('" + res.list[i].id + "')\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;'><i class=\"layui-icon\">&#xe640;</i></button>";
                                    html += "<a target='_blank' href=\"" + res.list[i].filepath + "\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;margin-right:10px;'><i class=\"layui-icon\">&#xe601;</i></a>";
                                    html += "</div>";
                                    html += "</li>";
                                }

                                $("#filelist").append(html);
                            }
                            
                        }
                        else layer.msg("项目附件获取失败，请刷新重试");
                    },
                    error: function (res) {
                        console.log(res);
                        layer.msg("项目附件获取失败，请联系技术人员");
                    }
                });
            }
        };

        //关闭弹窗
        $("#close").on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });

    </script>
</body>
</html>